import java.nio.channels.Pipe;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-10-28
 * Time: 19:50
 */
class Student implements Comparable<Student> {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name);
    }
}
public class Test {
    public static void main(String[] args) {
        Map<String,Integer> hashMap = new HashMap<>();
        hashMap.put("world",3);
        hashMap.put("shell",2);
        hashMap.put("apple",5);
        System.out.println(hashMap);
        Set<String> hashSet = new HashSet<>();
        hashSet.add("word");
        hashSet.add("shell");
        hashSet.add("apple");
        System.out.println(hashSet);
        //去重
        int[] nums = {1,2,4,3,5,2,1,2,3,1,3,2,8,1,4,5,4,6,4,3,9,5,7,5,8,7,9,6,6,4,3,4,3};
        HashSet<Integer> set = new HashSet<>();
        for(int i = 0;i < nums.length;i++) {
            set.add(nums[i]);
        }
        System.out.println(set);

        //找到这组数据中第一个重复的元素
        Set<Integer> set2 = new HashSet<>();
        for(int i = 0;i < nums.length;i++) {
            if(set2.contains(nums[i])) {
                System.out.println(nums[i]);
                break;
            }else {
                set2.add(nums[i]);
            }
        }
        //统计一篇英文阅读 每个单词出现的次数
        //20241105

    }
    public static void main4(String[] args) {
        Set<Student> set = new TreeSet<>();
        set.add(new Student("zhangsan",3));
        set.add(new Student("wangwu",4));
        set.add(new Student("lisi",5));
        Student[] arr = set.toArray(new Student[set.size()]);
        System.out.println("Elements in array:");
        for(Student stu : arr) {
            System.out.println(stu.name + " " + stu.age);
        }
        Set<String> s = new TreeSet<>();
        s.add("hello");
        s.add("world");
        s.add("bird");
        Iterator<String> iterator = s.iterator();
        while(iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }
    public static void main3(String[] args) {
        //TreeSet实现Set
        Set<String> treeSet = new TreeSet<>();
        treeSet.add("windows");
        treeSet.add("unix");
        treeSet.add("Linux");
        System.out.println("treeSet:" + treeSet);

        Set<String> set = new TreeSet<>();
        set.add("mac OS");
        System.out.println("set添加之前:" + set);
        set.addAll(treeSet);
        System.out.println("set添加之后:" + set);
        System.out.println("set中是否包含treeSet中的所有元素"+set.containsAll(treeSet));
        boolean res = set.contains("Linux");
        System.out.println(res);
        boolean rm = set.remove("mac OS");
        System.out.println("是否删除mac Os:" + rm);
        System.out.println("set删除mac Os之后" + set);
        System.out.println(set.size());
        treeSet.clear();
        System.out.println("set清理之后:" + treeSet);
        System.out.println(treeSet.isEmpty());
    }
    public static void main2(String[] args) {
        //TreeMap实现Map
        Map<String,Integer> treeMap = new TreeMap<>();
        treeMap.put("hello",3);
        treeMap.put("world",2);
        treeMap.put("beautiful",4);
        treeMap.put("bird",1);
        System.out.println("treeMap:" + treeMap);
        // 如果key存在，返回key所对应的value
        // 如果key不存在，返回null
        System.out.println(treeMap.get("bird"));
        //如果key存在，返回与key所对应的value，如果key不存在，返回一个默认值
        System.out.println(treeMap.getOrDefault("milk", 20));
        //删除key对应的映射关系
        System.out.println("删除前的treeMap:" + treeMap);
        Integer value = treeMap.remove("beautiful");
        System.out.println(value);
        System.out.println("删除key='beautiful'后的treeMap:" + treeMap);
        boolean res = treeMap.remove("hello",2);
        System.out.println("是否{hello=2}被删除" + res);
        System.out.println("更新后的treeMap:" + treeMap);
        //如果存在key =="bird" && value == 1的键值对则删除
        treeMap.remove("bird",1);
        //keySet是将map中的key防止在Set中返回的
        for(String str : treeMap.keySet()) {
            System.out.print(str + " ");
        }
        System.out.println();
        System.out.println("keys:" + treeMap.keySet());
        //values()是将map中的value放在collect的一个集合中返回的
        for(Integer num : treeMap.values()) {
            System.out.print(num + " ");
        }
        System.out.println();
        System.out.println("values:" + treeMap.values());
        // entrySet(): 将Map中的键值对放在Set中返回了
        Set<Map.Entry<String,Integer>> entrySet = treeMap.entrySet();
        for(Map.Entry<String,Integer> entry : entrySet ) {
            System.out.println(entry.getKey() + "-->" + entry.getValue());
        }
        System.out.println("key/value映射:" + treeMap.entrySet());
        System.out.println(treeMap.containsKey("bird"));
        System.out.println(treeMap.containsValue(3));

    }
    public static void main1(String[] args) {
        BinarySearchTree bst = new BinarySearchTree();
        bst.insert(14);
        bst.insert(5);
        bst.insert(1);
        bst.insert(9);
        bst.insert(17);
        bst.insert(12);
        bst.insert(7);
        bst.insert(6);
        bst.insert(10);

        BinarySearchTree.TreeNode ret = bst.search(9);
        System.out.println(ret.val);

        bst.remove(9);
    }
}
